package com.relife.core.easyui;



import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.alibaba.fastjson.JSONObject;
import com.relife.common.utils.StringUtil;


public class LeaveTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String code;
	private boolean select = false;
	private boolean text = false;
	private String name;
	private String updateName;
	private String remark;
	private Date updateDate;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append(this.getHtml());
			out.print(sb);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private StringBuffer getHtml() {
		StringBuffer sb = new StringBuffer("");
		String str = "{0:'正常',1:'周末',2:'节假日',3:'迟到',4:'带薪假',5:'病假',6:'出差',7:'事假',8:'培训',9:'旷工',10:'请假'}";
		JSONObject json = JSONObject.parseObject(str);

		if (select) {
			sb.append("<select id='" + name + "' name='" + name + "'>\n");
			Iterator<?> it = json.entrySet().iterator();
			while (it.hasNext()) {
				String key = it.next().toString();
				Object val = json.get(key);
				if (key.equals(code)) {
					sb.append("<option value='" + key + "' selected='selected'>" + val + "</option>\n");
				} else {
					sb.append("<option value='" + key + "'>" + val + "</option>\n");
				}
			}
			sb.append("</select>\n");
		} else if (text) {
			sb.append("<input type='hidden' id='" + name + "' name='" + name + "' value='" + code + "'>" + json.get(code));
		} else {
			String str1 = "{0:'／',1:'',2:'',3:'━',4:'¤',5:'○',6:'√',7:'＋',8:'◇',9:'✘',10:'☆'}";
			JSONObject obj1 = JSONObject.parseObject(str1);
			if (StringUtil.isNotEmpty(code)) {
				if (updateDate != null) {
					SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					sb.append("<font style='color:red'");
					sb.append(" codeName='" + json.get(code) + "'");
					sb.append(" updateDate='" + sd.format(updateDate) + "'");
					sb.append(" updateName='" + updateName + "'");
					sb.append(" remark='" + remark + "'");
					sb.append(">" + obj1.get(code) + "</font>");
				} else {
					sb.append("<font title='" + json.get(code) + "'>" + obj1.get(code) + "</font>");
				}
			}
		}
		return sb;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public boolean isSelect() {
		return select;
	}

	public void setSelect(boolean select) {
		this.select = select;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUpdateName() {
		return updateName;
	}

	public void setUpdateName(String updateName) {
		this.updateName = updateName;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public boolean isText() {
		return text;
	}

	public void setText(boolean text) {
		this.text = text;
	}

}
